package com.aipanda.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * 商品类
 * 
 * @param products [详细产品]好理解
 * @author wanyuxiang
 * @meaning 一个商品会对应多个详细产品<br/>
 *          比如同一款衣服[商品]，因为有对应多个尺码，颜色等组合[产品]<br/>
 *          形成1对多
 * 
 */
@Entity
@Table(name = "aipanda_goods")
public class Goods extends BaseEntity {
	private static final long serialVersionUID = -6977025562650112419L;
	private Set<Product> products = new HashSet<Product>();

	@OneToMany(mappedBy = "goods", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
	public Set<Product> getProducts() {
		return this.products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}

	/**
	 * method meaning: <br/>
	 * 返回当前这个商品所有产品的规格值 <br/>
	 * 规格：比如，颜色，尺码 <br/>
	 * 规格值：各种颜色，各种尺码
	 */
	@Transient
	public Set<SpecificationValue> getSpecificationValues() {
		Set<SpecificationValue> hashSet = new HashSet<SpecificationValue>();
		if (getProducts() != null) {
			Iterator<Product> iterator = getProducts().iterator();
			while (iterator.hasNext()) {
				Product product = iterator.next();
				hashSet.addAll(product.getSpecificationValues());
			}
		}
		return hashSet;
	}
}
